MQTT_Publish block

Short summary

Name

MQTT_Publish

→POU type

→function

Category

more system blocks, MQTT

Graphical interface

Available since

  • version 1.28.0 (for Neuron Power Engineer) and version 2.3.1301 of the →runtime system – initial variant

  • version 1.108.0 (for Neuron Power Engineer) and version 3.18.0 of the →runtime system – enhancement: data type STRING for existing input topic (instead of STRING[255]); data type MQTT_RC for output rc (instead of DINT); data type MQTT_SUBSCRIBER_STATE for return value (instead of SINT

(warning) This block is supported for →Raspberry Pi

Functionality

The block publishes a message for a given topic via the connection that is specified by the connection handler. The messages are published asynchronously.

(info) The data is transferred by means of an already existing MQTT broker (see "Preparing/Realizing data transfer via MQTT").

Inputs, outputs

 

Identifier

→Data type

Description

Inputs: 

ch

DINT

connection handler (as obtained by the MQTT_Connect block)

topic

STRING

topic for the message

payload

REF_TO BYTE

payload of the message 

len

DINT

length of the payload to publish 

Outputs:

rc

MQTT_RC

return code of involved MQTT functions as specified in data type MQTT_RC

Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.

See:

Example for usage within ST-editor

Program with selected calls of MQTT blocks
PROGRAM Program1
    VAR
        ioImageLocal : ARRAY[0..1023] OF BYTE;
        ioImageLocalLen : DINT := 2;
        ch : DINT := -1;
        cnt : DINT := 0;
        rc : MQTT_RC;
        state : MQTT_SUBSCRIBER_STATE := MQTT_SUBSCRIBER_STATE#INVALID;
    END_VAR
    
    state := MQTT_GetState(ch := ch, rc => rc, ENO => ENO);
    
    /* make sure MQTT connection is established and messages are received */
    IF state <> MQTT_SUBSCRIBER_STATE#CONNECTING AND state <> MQTT_SUBSCRIBER_STATE#CONNECTED THEN
        ch := MQTT_Connect(address := '192.168.1.107', clientId := 'RTS1', rc => rc, ENO => ENO);
    END_IF;
    IF ENO AND state = MQTT_SUBSCRIBER_STATE#CONNECTED THEN        
        /* prepare data to be transferred over MQTT */
        ioImageLocal[0] := 16#CA;
        ioImageLocal[1] := 16#FE;
        /* send updated io image to remote */
        MQTT_Publish(ch := ch, topic := 'sample_topic',
                        payload := REF(ioImageLocal[0]),
                        len := ioImageLocalLen,
                        rc => rc);
    END_IF;
END_PROGRAM

When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.